//获取应用实例
const app = getApp();
var WxParse = require('../../wxParse/wxParse.js');

Page({
  /**
  * 页面的初始数据
  */
  data: {
    currtab: 0,
    swipertab: [{ name: '每日爆款', index: 0 }, { name: '宣传素材', index: 1 }, { name: '汇客学堂', index: 2}],
    orderList1: [],
    orderList1isNull:false,
    orderList2: [],
    orderList2isNull:false,
    orderList3: [],
    orderList3isNull: false,
    orderList4: [],
    orderList4isNull: false,
    orderList5: [{
      order_num: '111111',
      detail: [{
        status: 4,
        order_id: '111',
        img: '/static/images/liveprod-p1.jpg',
        goods_name: '欧莱雅男士洗面奶',
        price: '32.05',
        num: '10',
        allprice: '25.45'
      },]
    }, {
      order_num: '222222',
      detail: [{
        status: 4,
        order_id: '111',
        img: '/static/images/liveprod-p1.jpg',
        goods_name: '欧莱雅男士洗面奶',
        price: '32.05',
        num: '10',
        allprice: '25.45'
      },]
    }, {
      order_num: '3333',
      detail: [{
        status: 4,
        order_id: '111',
        img: '/static/images/liveprod-p1.jpg',
        goods_name: '欧莱雅男士洗面奶',
        price: '32.05',
        num: '10',
        allprice: '25.45'
      }]
    }],
    orderList5isNull: false,
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    this.orderShow();
  },
  /**
  * 生命周期函数--监听页面初次渲染完成
  */
  onReady: function () {
    // 页面渲染完成
    this.getDeviceInfo();
  },

  onShow:function(){
    var that=this;
  },

  getDeviceInfo: function () {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeight
        })
        // console.log(that.data.deviceH)
      }
    })
  },
  /**
  * @Explain：选项卡点击切换
  */
  tabSwitch: function (e) {
    var that = this
    if (this.data.currtab === e.target.dataset.current) {
      return false
    } else {
      that.setData({
        currtab: e.target.dataset.current
      })
    }
  },

  tabChange: function (e) {
    // console.log(e.detail.current )
    this.setData({ currtab: e.detail.current })
    this.orderShow();
    // this.statistics();
  },

  orderShow: function () {
    let that = this
    switch (this.data.currtab) {
      case 0:
        that.getList1()
        break
      case 1:
        that.getList2()
        break
      case 2:
        that.getList3()
        break
    }
  },


//获取每日爆款商品列表
  getList1: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that=this;
    wx.request({
      url: app.globalData.api + '/app.php?c=Haodanku&a=selectedItem',
      data: {
        min_id: 1,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        wx.hideLoading();
        that.data.orderList1=[];
        console.log(res.data.data)
        if (res.data.data.length==0){
          console.log("noData")
          that.setData({
            orderList1isNull: true
          })
        }else{
            that.setData({
              orderList1: that.data.orderList1.concat(res.data.data),
            })
        }
        if (that.data.orderList1.length > 0) {
          that.setData({
            orderList1isNull: false
          })
        } else {
          that.setData({
            orderList1isNull: true
          })
        }
        console.log(that.data.orderList1)
      },fail(){
        wx.hideLoading();
      }
    })
    
  },

//宣传素材
  getList2: function () {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var that = this;
    that.data.orderList2 = [];
    wx.request({
      url: app.globalData.api + '/app.php?c=BbsArticle&a=getArticleList',
      data: {
        token: app.globalData.token,
        board_id: 2,
        p:1,
        per:20,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        wx.hideLoading();
        console.log(res)
        for (var i = 0; i < res.data.data.list.length; i++) {
          res.data.data.list[i].img = app.globalData.imgUrl + "" + res.data.data.list[i].img;
        }
        that.setData({
          orderList2: that.data.orderList2.concat(res.data.data.list),
        })
        console.log(that.data.orderList2)
        if (that.data.orderList2.length > 0) {
          that.setData({
            orderList2isNull: false
          })
        } else {
          that.setData({
            orderList2isNull: true
          })
        }
        // that.statistics();
      }, fail() {
        wx.hideLoading();
      }
    })
   
  },


//汇课学堂
  getList3: function () {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var that = this;
    that.data.orderList3 = [];
    wx.request({
      url: app.globalData.api + '/app.php?c=Article&a=getArticleList',
      data: {
        cat_id:5,
        p: 1,
        per: 20,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        wx.hideLoading();
        console.log(res)
        console.log(res.data.data.list.length)
        for (var i = 0; i < res.data.data.list.length; i++) {
          res.data.data.list[i].img = app.globalData.imgUrl + "" + res.data.data.list[i].img;
        }
        that.setData({
          orderList3: that.data.orderList3.concat(res.data.data.list),
        })
        if (that.data.orderList3.length > 0) {
          that.setData({
            orderList3isNull: false
          })
        } else {
          that.setData({
            orderList3isNull: true
          })
        }
      }, fail() {
        wx.hideLoading();
      }
    })
  },

  //点击事件  
  clickImg: function (e) {
    console.log(e)
    var imgUrl = e.currentTarget.dataset.src;
    wx.previewImage({
      urls: [imgUrl], //需要预览的图片http链接列表，注意是数组
      current: '', // 当前显示图片的http链接，默认是第一个
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
  * 生命周期函数--监听页面隐藏
  */
  onHide: function () {

  },

  /**
  * 生命周期函数--监听页面卸载
  */
  onUnload: function () {

  },

  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {

  },

  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {

  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {

  }
});
